﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MidApp
{
    public class ManageStock
    {
        /// <summary>
        ///Metoda vytvori novou relaci pro sledovani kreditu hrace v ramci tymu
        ///</summary>
        public static void CreateNewStock(int UserID, int TeamID)
        {
            AppLib.Stock newStock = new AppLib.Stock();
            var db = MidApp.LinqUse.DBConn;

            newStock.UserID = UserID;
            newStock.TeamID = TeamID;
            newStock.Credit = 0;

            db.Stocks.InsertOnSubmit(newStock);
            db.SubmitChanges();
        }
        /// <summary>
        ///Metoda zrusi ucet i s kredity, bdue se muset vyresit, co kdyz ma prachy jeste?.
        ///</summary>
        public static void RemoveStock(int UserID, int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;
            var aux = (from Stock in db.Stocks where Stock.UserID == UserID && Stock.TeamID == TeamID select Stock).First();

            MidApp.ManageIsStockDetail.DeleteDetails(TeamID, UserID);
            db.Stocks.DeleteOnSubmit(aux);
            db.SubmitChanges();
        }
        /// <summary>
        ///Metoda pro manipulaci s kredity hrace; pripocte nebo odecte hodnotu charge od aktualniho stavu.
        ///</summary>
        public static void ChangeCreditsBalance(int UserID, int TeamID, int Charge)
        {
            var db = MidApp.LinqUse.DBConn;
            var aux = (from Stock in db.Stocks where Stock.UserID == UserID && Stock.TeamID == TeamID select Stock).First();

            aux.Credit = aux.Credit + Charge;
            
            db.SubmitChanges();
        }
        /// <summary>
        ///Metoda vymaze kredit na 0.
        ///</summary>
        public static void ChangeCreditsBalanceToZero(int UserID, int TeamID)
        {
            var db = MidApp.LinqUse.DBConn;
            var aux = (from Stock in db.Stocks where Stock.UserID == UserID && Stock.TeamID == TeamID select Stock).First();

            aux.Credit = 0;

            db.SubmitChanges();
        }
        /// <summary>
        ///Metoda vraci list Stock hracu v danem tymu.
        ///</summary>
        public static List<AppLib.Stock> GetPlayers(int TeamID)
        {

            var db = MidApp.LinqUse.DBConn;
            var aux = (from Stock in db.Stocks where Stock.TeamID == TeamID select Stock).ToList();

            return aux;
        }
        /// <summary>
        ///Metoda vraci hrace v danem tymu.
        ///</summary>
        public static AppLib.Stock GetPlayer(int TeamID, int UserID)
        {

            var db = MidApp.LinqUse.DBConn;
            var aux = (from Stock in db.Stocks where Stock.TeamID == TeamID && Stock.UserID == UserID select Stock).First();

            return aux;
        }

    }
}
